﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainPageBottom : MonoBehaviour
{

    public Button dongfuBtn;
    public Button characterBtn;
    public Button zongmenBtn;
    public Button xiuXingBtn;

    public void OnShow()
    {
        dongfuBtn.onClick.RemoveAllListeners();
        characterBtn.onClick.RemoveAllListeners();
        zongmenBtn.onClick.RemoveAllListeners();
        xiuXingBtn.onClick.RemoveAllListeners();
        dongfuBtn.onClick.AddListener(() =>
        {
            UIManager.Instance.ShowPage(UINames.DongfuPage);
            RefreshBtnState(dongfuBtn);
        });
        characterBtn.onClick.AddListener(() =>
        {
            UIManager.Instance.ShowPage(UINames.CharacterPage);
            RefreshBtnState(characterBtn);
        });
        zongmenBtn.onClick.AddListener(() =>
        {
            UIManager.Instance.ShowPage(UINames.ZongmenPage);
            RefreshBtnState(zongmenBtn);
        });
        xiuXingBtn.onClick.AddListener(() =>
        {
            UIManager.Instance.ShowPage(UINames.XiuXingPage);
            RefreshBtnState(xiuXingBtn);
        });

        UIManager.Instance.ShowPage(UINames.DongfuPage);
        RefreshBtnState(dongfuBtn);

        MessageCenter.Instance.Register(MessageConsts.SHOW_DONG_FU, OnShowDongfu);
    }

    public void OnHide()
    {
        dongfuBtn.onClick.RemoveAllListeners();
        characterBtn.onClick.RemoveAllListeners();
        zongmenBtn.onClick.RemoveAllListeners();
        xiuXingBtn.onClick.RemoveAllListeners();
        MessageCenter.Instance.UnRegister(MessageConsts.SHOW_DONG_FU, OnShowDongfu);
    }

    private void RefreshBtnState(Button select)
    {
        var list = new List<Button>() {
            dongfuBtn ,
            characterBtn ,
            zongmenBtn ,
            xiuXingBtn ,
        };

        foreach (var btn in list)
        {
            btn.targetGraphic.color = btn == select ? Color.white : new Color(0.7f, 0.7f, 0.7f, 1f);
        }
    }

    private void OnShowDongfu(object param)
    {
        RefreshBtnState(dongfuBtn);
    }
}
